package com.IntelligentCommunity.community.controller;

import com.IntelligentCommunity.common.core.domain.R;
import com.IntelligentCommunity.common.core.utils.poi.ExcelUtil;
import com.IntelligentCommunity.common.core.web.controller.BaseController;
import com.IntelligentCommunity.common.core.web.domain.AjaxResult;
import com.IntelligentCommunity.common.core.web.page.TableDataInfo;
import com.IntelligentCommunity.common.log.annotation.Log;
import com.IntelligentCommunity.common.log.enums.BusinessType;
import com.IntelligentCommunity.common.security.annotation.RequiresPermissions;
import com.IntelligentCommunity.community.domain.RealDistrict;
import com.IntelligentCommunity.community.service.IRealDistrictService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.concurrent.CompletableFuture;

/**
 * 小区Controller
 *
 * @author zzpjx
 * @date 2024-07-19
 */
@RestController
@RequestMapping("/district")
public class RealDistrictController extends BaseController
{
    @Autowired
    private IRealDistrictService realDistrictService;

    /**
     * 查询小区列表
     */
    //    @RequiresPermissions("community:district:list")
    @GetMapping("/list")
    public TableDataInfo list(RealDistrict realDistrict)
    {
        startPage();
        List<RealDistrict> list = realDistrictService.selectRealDistrictList(realDistrict);  //查询
        return getDataTable(list);
    }

    @GetMapping("/listVo")
    public TableDataInfo listVo(RealDistrict realDistrict)
    {
        List<RealDistrict> list = realDistrictService.selectRealDistrictList(realDistrict);
        return getDataTable(list);
    }

    /**
     * 导出小区列表
     */
    @Log(title = "小区", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, RealDistrict realDistrict)
    {
        List<RealDistrict> list = realDistrictService.selectRealDistrictList(realDistrict);
        ExcelUtil<RealDistrict> util = new ExcelUtil<RealDistrict>(RealDistrict.class);
        util.exportExcel(response, list, "小区数据");
    }

    /**
     * 导入小区
     */
    @PostMapping( "/import")
    @Transactional
    public R handleFileUpload(@RequestParam("file") MultipartFile file) {

        ExcelUtil excelUtil = new ExcelUtil(RealDistrict.class);
        try {
            List<RealDistrict> list = excelUtil.importExcel(file.getInputStream());
            if(CollectionUtils.isEmpty(list)){
                throw new RuntimeException("文件内容为空");
            }
            int batchSize = 5000;
            int batchCount = (list.size() + batchSize - 1) / batchSize;
            for (int i = 0; i < batchCount; i++) {
                int fromIndex = i * batchSize;
                int toIndex = Math.min(fromIndex + batchSize, list.size());
                List<RealDistrict> batchList = list.subList(fromIndex, toIndex);
                CompletableFuture.runAsync(() -> realDistrictService.insertRealDistrictList(batchList));
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return R.ok();
    }

    /**
     * 获取小区详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(realDistrictService.selectRealDistrictInfoById(id));
    }

    /**
     * 新增小区
     */
    @RequiresPermissions("community:district:add")
    @Log(title = "小区", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody RealDistrict realDistrict)
    {
        return toAjax(realDistrictService.insertRealDistrict(realDistrict));
    }

    /**
     * 修改小区
     */
    @Log(title = "小区", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody RealDistrict realDistrict)
    {
        return toAjax(realDistrictService.updateRealDistrict(realDistrict));
    }

    /**
     * 删除小区
     */
    @Log(title = "小区", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(realDistrictService.deleteRealDistrictByIds(ids));
    }

    /**
     * 功能：小区下拉框列表查询
     * @return
     */
    @GetMapping("districtList")
    public R districtList(){
        List<RealDistrict> list = realDistrictService.districtList();
        return R.ok(list);
    }

    @GetMapping("/findById/{id}")
    public R<RealDistrict> findById(@PathVariable("id") Long id){
        RealDistrict realDistrict = realDistrictService.selectRealDistrictById(id);
        return R.ok(realDistrict);
    }

    /**
     * 根据区域id查询小区
     * @param areaId
     * @return
     */
    @GetMapping("/findByAreaId/{areaId}")
    public R findByAreaId(@PathVariable("areaId") Long areaId){
        List<RealDistrict> list = realDistrictService.selectRealDistrictByAreaId(areaId);
        return R.ok(list);
    }

    /**查询辖区下的所有小区*/
    @GetMapping("/getDistrictByAreaId/{id}")
    public R getDistrictByAreaId(@PathVariable("id") Long id){
        List<RealDistrict> list = realDistrictService.getDistrictByAreaId(id);
        return R.ok(list);
    }


}
